package com.zyl.basis.demo01;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;

/**
 * @author zhaoyl
 * @date 2024/5/27 12:45
 * @description 说明
 * 创建线程方式一：实现Callable，重写call方法。可以定义返回值，可以抛出异常
 */
public class TestThread3 implements Callable<String> {
    private int age;
    private String name;

    public TestThread3(int age, String name) {
        this.age = age;
        this.name = name;
    }

    @Override
    public String call() throws Exception {
        return "得到返回结果-> " + name+":"+age;
    }

    public static void main(String[] args) throws Exception {
        //创建线程池
        ExecutorService executor = Executors.newFixedThreadPool(2);
        //创建执行任务
        FutureTask<String> task1 = new FutureTask<>(new TestThread3(18,"小红"));
        FutureTask<String> task2 = new FutureTask<>(new TestThread3(20,"小明"));
        //提交执行
        executor.execute(task1);
        executor.execute(task2);
        //获取结果
        System.out.println(task1.get());
        System.out.println(task2.get());
       //关闭
        executor.shutdown();
    }
}
